﻿#include "logreader.h"
#include <QDebug>

LogReader::LogReader(QObject *parent)
    : QObject{parent}
{}

LogReader::LogReader(QString filePath)
{
    m_File = new QFile(filePath);
}

void LogReader::closeFile()
{
    m_File->close();
}

bool LogReader::readFile()
{
    // 读取json格式文件失败，返回
    if ( !m_File->open(QIODevice::ReadOnly)) {
        qDebug()<<m_File->errorString();
        return false;
    }

    // // 读取文件内容并关闭文件
    // if (m_InTextStream) delete m_InTextStream;
    // m_InTextStream = new QTextStream(m_File->readAll());
    // m_File->close();

    return true;
}

QString LogReader::readNextCycle()
{
    QString res;

    // 读取文件内容并关闭文件
    while(!m_File->atEnd()) {
        QByteArray bytes = m_File->readLine();
        // qDebug()<<m_File->pos();
        QString tmpStr = QString::fromUtf8(bytes);
        res += tmpStr + "\n";
        if (tmpStr.contains(m_CycleEndFlag)) break;
    }

    return res;
}

void LogReader::setCycleEndFlag(QString flag)
{
    m_CycleEndFlag = flag;
}
